import calendar
import hashlib
import datetime
from dateutil.relativedelta import relativedelta

HASH_FORMAT = "yjyx_%s_smssign"


class Utils:
    @staticmethod
    def calcSign(raw):
        hash = hashlib.md5()
        hash.update((HASH_FORMAT % raw).encode('utf-8'))
        return hash.hexdigest()

    @staticmethod
    def findUserByName(username, ucls):
        # find user mobile phone number
        user = ucls.objects.get_or_default(phonenumber=username)
        if not user:
            user = ucls.objects.get_or_default(user__username=username)
        return (user, ucls) if user else (None, None)

    @staticmethod
    def getTodayDatetime():
        """
        获取今天零点和24点
        :return:
        """
        now = datetime.datetime.now()
        zero_today = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second,
                                              microseconds=now.microsecond)
        last_today = zero_today + datetime.timedelta(hours=23, minutes=59, seconds=59)
        return zero_today, last_today

    @staticmethod
    def getWeekDatetime():
        """
        获取本周一零点和周日24点
        :return:
        """
        now = datetime.datetime.now()
        zero_week = now - datetime.timedelta(days=now.weekday(), hours=now.hour, minutes=now.minute, seconds=now.second,
                                             microseconds=now.microsecond)
        last_week = now + datetime.timedelta(days=6 - now.weekday(), hours=23 - now.hour, minutes=59 - now.minute,
                                             seconds=59 - now.second)

        return zero_week, last_week

    @staticmethod
    def getMonthDatetime():
        """
        获取本月一号零点和最后一天24点
        :return:
        """
        now = datetime.datetime.now()
        zero_month = datetime.datetime(now.year, now.month, 1)
        last_month = zero_month + relativedelta(months=1) - datetime.timedelta(days=1) + datetime.timedelta(
            hours=23, minutes=59, seconds=59)
        return zero_month, last_month

    @staticmethod
    def get_month_start_and_end_date(year, month):
        """
        获取指定年月的开始结束日期
        :param year: '2020'
        :param month: '08'
        :return:第一天日期/最后一天日期
        """
        year, month = int(year), int(month)
        end = calendar.monthrange(year, month)[1]
        # start_date = datetime.date(year=year, month=month, day=1).strftime("%Y-%m-%d")
        # end_date = datetime.date(year=year, month=month, day=end).strftime("%Y-%m-%d")
        start_date = datetime.datetime(year=year, month=month, day=1, hour=0, minute=0, second=0)
        end_date = datetime.datetime(year=year, month=month, day=end, hour=23, minute=59, second=59)
        return start_date, end_date

    @staticmethod
    def getYesterday():
        """
        获取昨天日期
        :return:2021-02-24
        """
        today = datetime.date.today()
        oneday = datetime.timedelta(days=1)
        yesterday = today - oneday
        return yesterday
